/*
 * Subclass of the uc.edu.server.Message class.
 * Adds an ID and a timestamp.
 * The ID is a long, allowing for plenty of messages.
 * An int, allowing more than two billion messages, might
 * be more than enough.
 * The timestamp is a java.sql.Timestamp object, suitable
 * for storing in a db.
 */
package uc.edu.server;

import java.sql.Timestamp;
import java.util.Date;

/**
 *
 * @author tony
 */
public class MessageWithID extends Message
    implements Comparable<MessageWithID>
{
    // our own id number:
    private long id;
    // and timestamp:
    private Timestamp timestamp;
    
    // Accessors and mutators:
    public long getID() { return id; }
    public void setID(long newID) { id = newID; }
    public Timestamp getTimestamp() { return timestamp; }
    public void setTimestamp(Timestamp ts) { timestamp = ts; }
    
    /**
     * Constructor implicitly calls default constructor
     * of parent class and then explicitly sets ID to 0.
     * Actual value of id will be assigned when this
     * message is added to server's message store.
     */
    public MessageWithID()
    {
        // Initialize to ID 0 and date/time now:
        id = 0L;
        Date d = new Date();
        timestamp = new Timestamp(d.getTime());
    }

    /**
     * This constructor, in addition, sets
     * our fields based on fields of an
     * already-existing Message object.
     */
    public MessageWithID(Message msg)
    {
        this(); // call empty constructor
        this.setContent(msg.getContent());
        this.setRecipients(msg.getRecipients());
        this.setSender(msg.getSender());
    }

    /**
     * Compare based on message ID.
     * @param o
     * @return 
     */
    @Override
    public int compareTo(MessageWithID o)
    {
        // Don't simply cast this.getID() - o.getID() to
        // an integer -- if the difference is greater than
        // MAX_INT, that won't work.
        if (this.getID() > o.getID()) { return 1; }
        if (this.getID() < o.getID()) { return -1; }
        return 0;
    }
} // end class MessageWithID
